package security;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.access.BeanFactoryLocator;
import org.springframework.beans.factory.access.BeanFactoryReference;
import org.springframework.context.ApplicationContext;
import org.springframework.context.access.ContextSingletonBeanFactoryLocator;

import biz.AuthCheckBiz;

/**
 * @author ben
 */
public class CorePathToAuth {

	// 用户一览ꗗ
	public static final String PAGENO_SYSTEM_USER_LIST = "user.list";

	// 编辑用户
	public static final String PAGENO_SYSTEM_USER_EDIT = "user.edit";

	// 删除用户
	public static final String PAGENO_SYSTEM_USER_DELETE = "user.delete";

	public static long getAuth(HttpServletRequest request) {
		String dispatch = request.getParameter("dispatch");
		if (dispatch == null) {
			dispatch = "init";
		}
		String contextpath = request.getContextPath();
		String path = request.getRequestURI();
		path = path.substring(contextpath.length(), (path.length() - 3));

		String pageNo = "";
		// 用户一览ꗗ
		if (path.equals("/user/list")
				&& (dispatch.equals("init") || dispatch.equals("search"))) {
			pageNo = PAGENO_SYSTEM_USER_LIST;
		}

		return getAuthByPageNo(pageNo);
	}

	public static long getAuthByPageNo(String pageNo) {
		
		BeanFactoryLocator locator = ContextSingletonBeanFactoryLocator
				.getInstance();
		BeanFactoryReference ref = locator.useBeanFactory("context");
		ApplicationContext context = (ApplicationContext) ref.getFactory();

		AuthCheckBiz authCheckBiz = (AuthCheckBiz) context
				.getBean("authCheckBiz");

		return authCheckBiz.findAuthByPageNo(pageNo);
	}
}
